package com.monlyu.tag.extend;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.monlyu.bo.UserBo;
import com.monlyu.listener.InitDataListener;
import com.monlyu.tag.BaseTag;
import com.monlyu.util.ArrayUtil;
import com.monlyu.util.FtlUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.vo.User;

/********************************************************************************
 * 
 * <b>功能描述☞</b><br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.tag.extend.DropUserTag.java<br>
 * <b>开发日期☞</b>2010-3-2 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 * 
 ********************************************************************************/
public class DropUserTag extends BaseTag {

	private String name;
	private String cssClass;
	private Object value;
	private boolean multiple;
	private String headValue;
	private String headKey;
	private boolean canSelectAll = true;
	private String callback;
	private boolean showSelected;

	public void setCanSelectAll(boolean canSelectAll) {
		this.canSelectAll = canSelectAll;
	}

	private Set<String> CACHE = Sets.newHashSet();// 创建缓存

	public void setCallback(String callback) {
		this.callback = callback;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}

	public void setHeadValue(String headValue) {
		this.headValue = headValue;
	}

	public void setHeadKey(String headKey) {
		this.headKey = headKey;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	@Override
	public int doStartTag() {
		try {
			write2Page(FtlUtil.render2Ftl(toMap(), "dropUser.ftl"));
		} catch (Exception e) {
			e.printStackTrace();
			name = cssClass = null;
			value = null;
		}
		return SKIP_BODY;
	}

	private Map<String, Object> toMap() {
		Map<String, Object> map = Maps.newHashMap();
		Object object = getValueFromValueStack(name);
		value = object;
		map.put("canSelAll", canSelectAll);
		if (multiple) {
			map.put("data", buildMultipleTree());
		} else {
			map.put("class", cssClass);
			UserBo userBo = (UserBo) InitDataListener.getBeanById("userBo");
			map.put("data", setOption(userBo.findUsers()));
		}
		map.put("muilt", multiple);
		if (!StringUtil.isEmpty(callback)) {
			map.put("callback", callback);
		}
		map.put("showSelected", showSelected);
		map.put("name", name);
		if (!StringUtil.isTrimEmpty(headValue)) {
			map.put("headValue", headValue);
		}
		if (!StringUtil.isTrimEmpty(headKey)) {
			map.put("headKey", headKey);
		}
		return map;
	}

	private String setOption(List<User> uses) {
		StringBuilder builder = new StringBuilder();
		for (User user : uses) {
			String selcted = (value != null && user.getId().intValue() == Integer.parseInt(value.toString())) ? "selected='selected'"
					: "";
			builder.append("<option value='" + user.getId() + "' " + selcted + " >" + user.getName() +"("+user.getNickName()+")"+ "</option>");
		}
		return builder.toString();
	}

	private String buildMultipleTree() {
		StringBuilder builder = new StringBuilder();
		builder.append("<select multiple='multiple' id='drop" + name + "' ");
		builder.append(StringUtil.isEmpty(name) ? "" : (" name='" + name + "'"));
		builder.append(StringUtil.isEmpty(cssClass) ? "" : (" class='" + cssClass + "'"));
		builder.append(">");
		CACHE.clear();
		if (value != null) {
			CACHE.addAll(ArrayUtil.toSet(value.toString().split(",")));
		}
		List<User> finds = ((UserBo) InitDataListener.getBeanById("userBo")).findUsers();
		for (User user : finds) {
			builder.append("<option value='" + user.getId() + "' ");
			builder.append(selectCheck(user.getId().toString()) ? " selected='selected'" : "");
			builder.append(">" + user.getName() +"("+user.getNickName()+")</option>");
		}
		CACHE.clear();
		return builder.toString();
	}

	private boolean selectCheck(String uid) {
		return CACHE.contains(uid);
	}

	public void setShowSelected(boolean showSelected) {
		this.showSelected = showSelected;
	}
}
